﻿using Infrastructure.Excel;
using Infrastructure.Operator;
using Infrastructure.Web;
using MyData;
using MyData.Dto;
using MyWeb.App_Start;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace MyWeb.Controllers
{
    public class AccessRecordController : BaseController
    {
        // GET: AccessRecord
        [HandlerLogin]
        public ActionResult Index()
        {
            ViewBag.Mobiles = mobileApp.GetMobiles();
            return View();
        }
        [HandlerLogin]
        public ActionResult CheckIn()
        {
            return View();
        }
        [HandlerLogin]
        public ActionResult HighchartView()
        {
            return View();
        }
        [HandlerLogin]
        public ActionResult PhotoView(Guid id)
        {
            var model = AccessRecordApp.GetAccessRecord(id);
            if (PublicInfo.Version == (int)EnumVersion.淮安 && !string.IsNullOrEmpty(model.PicPath))
            {
                if (model.PicPath.StartsWith(","))
                {
                    model.PicPath = model.PicPath.Substring(1, model.PicPath.Length - 1);
                }
                model.PicPath = "data:image/jpeg;base64," + model.PicPath;
            }
            return View(model);
        }

        public ActionResult XXCheckRecord()
        {
            return View();
        }

        public ActionResult GetAccessRecord(QueryListModel query, string NickName, Guid MobileId, string StartDate, string EndDate, int OrderName, int OrderType)
        {
            DateTime startDate = DateTime.Parse(StartDate).Date;
            DateTime endDate = DateTime.Parse(EndDate).Date.AddDays(1);
            var result = AccessRecordApp.GetAccessRecord(query, NickName, MobileId, startDate, endDate, OrderName, OrderType);
            return Json(result, JsonRequestBehavior.AllowGet);
        }
        public ActionResult GetCheckIn(QueryListModel query, string StartDate, string EndDate)
        {
            DateTime startDate = DateTime.Parse(StartDate).Date;
            DateTime endDate = DateTime.Parse(EndDate).Date.AddDays(1);
            var result = AccessRecordApp.GetCheckIn(query, startDate, endDate);
            return Json(result, JsonRequestBehavior.AllowGet);
        }
   
        
        public ActionResult GetXXCheckRecord(QueryListModel query, string StartDate, string EndDate)
        {
            DateTime startDate = DateTime.Parse(StartDate).Date;
            DateTime endDate = DateTime.Parse(EndDate).Date.AddDays(1);
            var result = AccessRecordApp.GetXXCheckRecord(query, startDate, endDate);
            return Json(result, JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public JsonResult AccessRecordDelete(string Id)
        {
            return base.Try(() =>
            {
                AccessRecordApp.AccessRecordDelete(Guid.Parse(Id));
            });
        }
        [HttpPost]
        public JsonResult AccessRecordBatchDelete(List<Guid> Ids)
        {
            return base.Try(() =>
            {
                AccessRecordApp.AccessRecordBatchDelete(Ids);
            });
        }

        [HttpPost]
        public JsonResult GetAccessRecordJson(string StartDate, string EndDate)
        {
            HighData highData = new HighData();
            var result = new AjaxResult<HighData>();
            return base.Try<HighData>(() =>
            {


                string[] colors = new string[] { "red", "green", "blue", "yellow", "black" };
                DateTime startDate = DateTime.Parse(StartDate);
                DateTime endDate = DateTime.Parse(EndDate).AddDays(1);
                int length = (endDate - startDate).Days;
                var lables = new string[length];
                for (int i = 0; i < length; i++)
                {
                    lables[i] = startDate.AddDays(i).ToString("yyyy-MM-dd");
                }
                highData.LabelList = lables;
                var mobiles = mobileApp.GetMobiles();
                var records = AccessRecordApp.GetRecordChartDtos(startDate, endDate);


                for (int i = 0; i < mobiles.Count; i++)
                {
                    dataset dataset = new dataset();
                    dataset.backgroundColor = colors[i % 5];
                    dataset.borderColor = colors[i % 5];
                    dataset.label = mobiles[i].Name;
                    int[] data = new int[length];
                    for (int j = 0; j < length; j++)
                    {
                        var record = records.FirstOrDefault(c => c.MobileId == mobiles[i].Id && c.CreateDate == lables[j]);
                        if (record == null) data[j] = 0;
                        else
                        {
                            data[j] = record.Count;
                        }

                    }
                    dataset.data = data;
                    highData.datasets.Add(dataset);
                }
                result.data = highData;

            }, result);
        }
        public FileResult ExcelImport()
        {
            List<CheckInDto> checkInDtos = Session["Data"] as List<CheckInDto>;
            if (checkInDtos == null) checkInDtos = new List<CheckInDto>();
            string filename = $"~/Export/{DateTime.Now.ToString("yyyyMMddHHmmss")}.xlsx";
            filename = Server.MapPath(filename);
            EpplusHelper.SaveToExcel(checkInDtos, filename);
            return File(filename, "application/octet-stream", $"{DateTime.Now.ToString("yyyyMMddHHmmss")}.xls");

        }
        public FileResult ExcelImport1()
        {

            List<AccessRecordDto> checkInDtos = Session["Data"] as List<AccessRecordDto>;
            if (checkInDtos == null) checkInDtos = new List<AccessRecordDto>();
            string filename = $"~/Export/{DateTime.Now.ToString("yyyyMMddHHmmss")}.xlsx";
            filename = Server.MapPath(filename);
            EpplusHelper.SaveToExcel(checkInDtos, filename);
            return File(filename, "application/octet-stream", $"{DateTime.Now.ToString("yyyyMMddHHmmss")}.xls");

        }
        public FileResult XXExcelImport()
        {
            List<XXCheckRecord> checkInDtos = Session["Data"] as List<XXCheckRecord>;
            if (checkInDtos == null) checkInDtos = new List<XXCheckRecord>();
            string filename = $"~/Export/{DateTime.Now.ToString("yyyyMMddHHmmss")}.xlsx";
            filename = Server.MapPath(filename);
            EpplusHelper.SaveToExcel(checkInDtos, filename,
                new string[] { "序号", "姓名", "迟到(次)", "早退(次)", "未签(次)", "旷工(天)", "公假(天)", "事假(天)", "病假(天)", "产假(天)", "婚假(天)", "丧假(天)" ,"备注"});
            return File(filename, "application/octet-stream", $"{DateTime.Now.ToString("yyyyMMddHHmmss")}.xls");

        }
    }
    public class HighData
    {
        public string[] LabelList { get; set; }
        // public double[] ValueList { get; set; }
        public List<dataset> datasets { get; set; } = new List<dataset>();
    }
    public class dataset
    {
        public string label { get; set; }
        public string backgroundColor { get; set; }
        public string borderColor { get; set; }
        public bool fill { get; set; } = false;
        public int[] data { get; set; }
    }

}